package cn.net.shuxing.yifu.common.service;

import cn.net.shuxing.yifu.common.bean.BaseBean;
import cn.net.shuxing.yifu.common.util.LocalDateTimeUtil;
import cn.net.shuxing.yifu.common.util.LocalTimeUtil;
import cn.net.shuxing.yifu.common.util.TimeExample;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.entity.Example;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 在BaseService的基础上扩展了一些时间相关的操作
 *
 * @param <B> bean的类型
 * @param <M> bean对应的mapper的类型
 */
public abstract class HistoryService<B extends BaseBean, M extends Mapper<B>> extends BaseService<B, M> {

    private String property = "createTime";
    public HistoryService() {
        Class<B> beanClass = getBeanClass();
        Field createTime = null;
        try {
            createTime = beanClass.getDeclaredField("createTime");
            property = "createTime";

        } catch (NoSuchFieldException e) {
            property = "createDate";
        }

    }

    public List<B> getByToday() {
        Class<B> beanClass = getBeanClass();
        TimeExample example = new TimeExample(getBeanClass());
        try {
            Field createTime = beanClass.getDeclaredField("createTime");
            if (createTime != null) {
                Class<?> type = createTime.getType();
                if (type == LocalDateTime.class) {
                    LocalDateTime todayDateTime = LocalDateTimeUtil.createLocalDateTime(LocalDate.now(), LocalTimeUtil.createLocalTime(0, 0, 0, 0));
                    Example.Criteria criteria = example.getCriteria();
                    criteria.andGreaterThan(property, todayDateTime);
                } else if (type == LocalDate.class) {
                    example.equalsToday(property);
                }
            } else {
                Field createDate = beanClass.getDeclaredField("createDate");
                if (createDate != null) {
                    Class<?> type = createDate.getType();
                    if (type == LocalDate.class) {
                        example.equalsToday("createDate");
                    }
                }
            }
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

        return getsByExample(example);
    }

    /**
     * 获取最近count年的bean信息
     *
     * @param count 要获取最近n年bean信息的天数
     * @return
     */
    public List<B> getByYearCount(int count) {
        TimeExample example = new TimeExample(getBeanClass());
        example.yearhGreaterThan(property, count);

        return getsByExample(example);
    }

    /**
     * 获取最近count月的bean信息
     *
     * @param count 要获取最近n月bean信息的天数
     * @return
     */
    public List<B> getByMonthCount(int count) {
        TimeExample example = new TimeExample(getBeanClass());
        example.monthGreaterThan(property, count);

        return getsByExample(example);
    }

    /**
     * 获取最近count天的bean信息
     *
     * @param count 要获取最近n天bean信息的天数
     * @return
     */
    public List<B> getByDayCount(int count) {
        TimeExample example = new TimeExample(getBeanClass());
        example.dayGreaterThan(property, count);
        return getsByExample(example);
    }

    /**
     * 获取最近count小时的bean信息
     *
     * @param count 要获取最近n小时bean信息的天数
     * @return
     */
    public List<B> getByHourCount(int count) {
        TimeExample example = new TimeExample(getBeanClass());
        example.hourGreaterThan(property, count);

        return getsByExample(example);
    }

    /**
     * 获取最近count分钟的bean信息
     *
     * @param count 要获取最近n分钟bean信息的天数
     * @return
     */
    public List<B> getByMinuteCount(int count) {
        TimeExample example = new TimeExample(getBeanClass());
        example.minuteGreaterThan(property, count);

        return getsByExample(example);
    }

    /**
     * 获取最近count秒的bean信息
     *
     * @param count 要获取最近n秒bean信息的天数
     * @return
     */
    public List<B> getBySecondCount(int count) {
        TimeExample example = new TimeExample(getBeanClass());
        example.secondGreaterThan(property, count);

        return getsByExample(example);
    }


    public void setProperty(String property) {
        this.property = property;
    }

    public List<B> getThisMonth() {
        TimeExample example = new TimeExample(getBeanClass());
        LocalDateTime thisMonthFirstDay = LocalDateTimeUtil.getThisMonthFirstDay();
        example.getCriteria().andGreaterThanOrEqualTo(property, thisMonthFirstDay);
        List<B> beans = getsByExample(example);
        return beans;
    }


}
